<?php
date_default_timezone_set( "Asia/Shanghai" );
$fileName = "msg.txt";
$msg = [];
if (file_exists( $fileName )) {
    $str = file_get_contents( $fileName );
    if (strlen( $str ) > 0) {
        $msg = unserialize( $str );
    }
}
if (isset( $_POST["submitMsg"] )) {
    $userName = $_POST["userName"];
    $title = strip_tags( $_POST["title"] );
    $content = strip_tags( $_POST["content"] );
    $time = time();
    $data = compact( 'userName', 'title', 'content', 'time' );
    array_push( $msg, $data );
    $msg = serialize( $msg );
    if (file_put_contents( $fileName, $msg )) {
        echo "<script>
            alert('留言添加成功')
            location.href='submit.php'
            </script>";
    } else {
        echo "<script>
            alert('留言添加失败')
            location.href='submit.php'
            </script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Bootstrap 4, from LayoutIt!</title>

    <meta name="description" content="Source code generated using layoutit.com">
    <meta name="author" content="LayoutIt!">

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

</head>
<body>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="page-header">
                <h1>
                    LayoutIt! <small>Interface Builder for Bootstrap</small>
                </h1>
            </div>
            <h2>
                Heading
            </h2>
            <p>
                Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris
                condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis
                euismod. Donec sed odio dui.
            </p>
            <p>
                <a class="btn" href="#">View details »</a>
            </p>
            <?php if (is_array( $msg ) and count( $msg ) > 0): ?>
                <table class="table">
                    <thead>
                    <tr>
                        <th>
                            编号
                        </th>
                        <th>
                            用户
                        </th>
                        <th>
                            标题
                        </th>
                        <th>
                            时间
                        </th>
                        <th>
                            内容
                        </th>
                    </tr>
                    </thead>


                    <tbody>
                    <?php $i = 1;
                    foreach ($msg as $item): ?>
                        <tr class="table-danger">
                            <td>
                                <?php echo $i++ ?>
                            </td>
                            <td>
                                <?php echo $item['userName'] ?>
                            </td>
                            <td>
                                <?php echo $item['title'] ?>
                            </td>
                            <td>
                                <?php echo date( "Y-m-d H:i:s", $item['time'] ) ?>
                            </td>
                            <td>
                                <?php echo $item['content'] ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
            <form role="form" action="#" method="post">
                <div class="form-group">
                    <p>请留言</p>
                    <label for="exampleInputEmail1">
                        用户名
                    </label>
                    <input type="text" required="required" name="userName" class="form-control" id="exampleInputEmail1"
                           style="width: 50%">
                </div>
                <div class="form-group">

                    <label for="exampleInputPassword1">
                        标题
                    </label>
                    <input type="text" required="required" name="title" class="form-control" id="exampleInputPassword1"
                           style="width: 50%">
                </div>
                <div class="form-group">

                    <label for="exampleInputFile">
                        内容
                    </label>
                    <textarea name="content" required="required" cols="100" rows="10" class="form-control-file"
                              id="exampleInputFile"></textarea>
                </div>
                <button type="submit" class="btn btn-primary" name="submitMsg">
                    发布留言
                </button>
            </form>
        </div>
    </div>
</div>

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/scripts.js"></script>
</body>
</html>